﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DAL;
using Entity;
public partial class admin_SysUserList : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView("");
        }
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    /// <param name="sqlwhere">条件</param>
    /// <param name="sort">排序</param>
    /// <param name="sort"></param>
    private void BindGridView(string sqlwhere)
    {
        ViewState["strWhere"] = sqlwhere;//条件
        ///根据权限加载
        int recordCount = 0;
        recordCount = SysUserDAL.Instance.SelectUserCount(sqlwhere);//根据条件，获取信息的条数
        SysUserInfoPage.RecordCount = recordCount;//分页赋值
        List<SysUser> list = SysUserDAL.Instance.SelectUserInfo(SysUserInfoPage.PageSize, SysUserInfoPage.CurrentPageIndex, sqlwhere);
        grdSysUserInfo.DataSource = list;
        grdSysUserInfo.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (txtKey.Text.Trim() != "")
        {
            BindGridView(" user_name like '%" + txtKey.Text.Trim() + "%' or login_name  like '%" + txtKey.Text.Trim() + "%' ");
        }
        else
        {
            BindGridView("");
        }
    }
    

    /// <summary>
    /// 数据绑定事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grdSysUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //e.Row.Attributes.Add("onmouseover", "if(this!=prevselitem){this.style.backgroundColor='#F3FFE3'}");//当鼠标停留时更改背景色
            //e.Row.Attributes.Add("onmouseout", "if(this!=prevselitem){this.style.backgroundColor='#FFFFFF'}");//当鼠标移开时还原背景色
            //e.Row.Attributes.Add("onclick", e.Row.ClientID.ToString() + ".checked=true;selectx(this)");//点击行变色

            e.Row.Attributes["style"] = "Cursor:hand"; //设置悬浮鼠标指针形状为"小手"
            Label lblTemp = (Label)e.Row.FindControl("lblNum");
            lblTemp.Text = (e.Row.RowIndex + 1).ToString();
            // + SysUserInfoPage.PageSize * (SysUserInfoPage.CurrentPageIndex - 1)).ToString();

            //Label lblUserName = (Label)e.Row.FindControl("lblUserName");
            Label lblLoginName = (Label)e.Row.FindControl("lblLoginName");
            Label lblUserName = (Label)e.Row.FindControl("lblUserName");
            Label lblUserStatus = (Label)e.Row.FindControl("lblState");
            HiddenField hfID = (HiddenField)e.Row.FindControl("hfID");
            //重置密码
            LinkButton linkRePassWord = (LinkButton)e.Row.FindControl("lnkRePass");
            if (linkRePassWord != null)
            {
                linkRePassWord.CommandArgument = hfID.Value;
                linkRePassWord.Attributes["onclick"] = "return SetRePassWord('" + lblLoginName.Text.Trim() + "');";
            }

            //停/启用账号
            LinkButton linkDisabled = (LinkButton)e.Row.FindControl("lnkDisabled");
            if (linkDisabled != null)
            {
                if (lblUserStatus.Text == "1" || lblUserStatus.Text == "正常")
                {
                    linkDisabled.Text = "停用";
                    linkDisabled.CommandArgument = hfID.Value + "#" + "0";
                    lblUserStatus.Text = "正常";
                    linkDisabled.Attributes["onclick"] = "return SetUserDisabled('" + lblLoginName.Text.Trim() + "',0);";

                }
                else
                {
                    linkDisabled.Text = "启用";
                    linkDisabled.CommandArgument = hfID.Value + "#" + "1";
                    lblUserStatus.Text = "停用";
                    linkDisabled.Attributes["onclick"] = "return SetUserDisabled('" + lblLoginName.Text.Trim() + "',1);";

                }

            }
            LinkButton lnkMenu = (LinkButton)e.Row.FindControl("lnkMenu");
            //修改用户
            if (lnkMenu != null)
            {
                lnkMenu.CommandArgument = lblLoginName.Text.Trim() + "#" + lblUserName.Text;
            }
        }

    }

    #region 分页
    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ChangePage(object sender, EventArgs e)
    {
        BindGridView(ViewState["strWhere"].ToString());
    }
    #endregion
    protected void grdSysUserInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "repass")
        {
            //
            if (SysUserDAL.Instance.RePassWord(e.CommandArgument.ToString()))
            {
                Alert("已成功将密码重置为:666666 ！");
            }
            else
            {
                Alert("重置失败，请重新操作！");
            }
            BindGridView(ViewState["strWhere"].ToString());
        }
        else if (e.CommandName == "lkdisabled")
        {
            string[] strs = e.CommandArgument.ToString().Split('#');
            string str = strs[1] == "0" ? "停用" : "启用";
            if (SysUserDAL.Instance.setUserDisabled(strs[0], strs[1]))
            {
                Alert(str + "成功！");
            }
            else
            {
                Alert(str + "失败，请重新操作！");
            }
            BindGridView(ViewState["strWhere"].ToString());
        }
        else if (e.CommandName == "lnkMenu")
        {
            string[] strs = e.CommandArgument.ToString().Split('#');
            Session.Add("SysUserLoginName", strs[0]);
            Session.Add("SysUserName", strs[1]);
            ShowModalDialog("SysUserOrFun.aspx", "300", "400");
        }
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtKey.Text = "";
        BindGridView("");
    }
}